//
//  SQMyViewController.m
//  pai
//
//  Created by pro on 14-10-30.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQMyViewController.h"
#import "UserInfo.h"
#import "Global.h"
#import "SQLoginViewController.h"

#import "SQAboutViewController.h"
#import "SQAddressViewController.h"
#import "SQPointsViewController.h"
#import "SQMyAccountViewController.h"
#import "SQMyNoticeViewController.h"
#import "SQMyRechargeViewController.h"

#import "SQOrderListViewController.h"
#import "SQAddressListViewController.h"
#import "SQNoticeListViewController.h"

#import "SQMySQL.h"

#import "MKNumberBadgeView.h"

@interface SQMyViewController ()

@property (retain) MKNumberBadgeView* badgeOne;
@property (retain) MKNumberBadgeView* badgeTwo;

@end

@implementation SQMyViewController

@synthesize scrollView = _scrollView;
@synthesize accountBtn = _accountBtn;
@synthesize orderBtn = _orderBtn;
@synthesize noticeBtn = _noticeBtn;
@synthesize rechargeBtn = _rechargeBtn;
@synthesize pointsBtn = _pointsBtn;
@synthesize addressBtn = _addressBtn;
@synthesize aboutBtn = _aboutBtn;
@synthesize quitBtn = _quitBtn;

@synthesize badgeOne;
@synthesize badgeTwo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"我的"];
    
    [self initListener];
    
    NSLog(@"view did load:%f",self.scrollView.frame.origin.y);
    
    //[self checkLogin];
}
-(void)initListener
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, [[UIScreen  mainScreen] bounds].size.height - 44 - self.MyNavigationBar.frame.origin.y-self.MyNavigationBar.frame.size.height)];
    [self.view addSubview:_scrollView];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = NO;
    _scrollView.scrollEnabled = YES;
    
    _accountBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 300, 53)];
    _accountBtn.tag = 0;
    [_accountBtn setBackgroundImage:[UIImage imageNamed:@"account"] forState:UIControlStateNormal];
    [_accountBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_accountBtn];
    
    
    _orderBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 90, 300, 53)];
    _orderBtn.tag = 1;
    [_orderBtn setBackgroundImage:[UIImage imageNamed:@"order"] forState:UIControlStateNormal];
    [_orderBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_orderBtn];
    self.badgeOne = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(_orderBtn.frame.size.width - 80, 5,44,40)];
    [_orderBtn addSubview:self.badgeOne];
    
    _noticeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 143, 300, 53)];
    _noticeBtn.tag = 2;
    [_noticeBtn setBackgroundImage:[UIImage imageNamed:@"notice"] forState:UIControlStateNormal];
    [_noticeBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_noticeBtn];
    self.badgeTwo = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(_noticeBtn.frame.size.width - 80, 5,44,40)];
    [_noticeBtn addSubview:self.badgeTwo];
    
    _rechargeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 196, 300, 53)];
    _rechargeBtn.tag = 3;
    [_rechargeBtn setBackgroundImage:[UIImage imageNamed:@"recharge"] forState:UIControlStateNormal];
    [_rechargeBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_rechargeBtn];
    
    _pointsBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 249, 300, 53)];
    _pointsBtn.tag = 4;
    [_pointsBtn setBackgroundImage:[UIImage imageNamed:@"points"] forState:UIControlStateNormal];
    [_pointsBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_pointsBtn];
    
    _addressBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 302, 300, 53)];
    _addressBtn.tag = 5;
    [_addressBtn setBackgroundImage:[UIImage imageNamed:@"address"] forState:UIControlStateNormal];
    [_addressBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_addressBtn];
    
    _aboutBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 355, 300, 53)];
    _aboutBtn.tag = 6;
    [_aboutBtn setBackgroundImage:[UIImage imageNamed:@"about"] forState:UIControlStateNormal];
    [_aboutBtn addTarget:self action:@selector(onActionClickfd:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_aboutBtn];
    
    _quitBtn = [[UIButton alloc]initWithFrame:CGRectMake(81, 420, 158, 38)];
    [_quitBtn setBackgroundImage:[UIImage imageNamed:@"quit"] forState:UIControlStateNormal];
    [_quitBtn addTarget:self action:@selector(doActionQuit:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:_quitBtn];
    
    
    _scrollView.contentSize = CGSizeMake(320,_quitBtn.frame.origin.y + 50);
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(changebadgeValue:) name:@"ACS_ERR_UNREAD_NOTE" object:nil];
    
    [self showbadge];
}
-(void)changebadgeValue:(NSNotification*)notification
{
    NSLog(@"my list ACS_ERR_UNREAD_NOTE");
    [self showbadge];
}
-(void)onActionClickfd:(UIButton*)btn
{
    switch (btn.tag) {
        case 0:{
            SQMyAccountViewController *vc = [[SQMyAccountViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 1:{
            SQOrderListViewController *vc = [[SQOrderListViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;}
        case 2:{
            SQNoticeListViewController* vc = [[SQNoticeListViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;}
        case 3:{
            SQMyRechargeViewController* vc = [[SQMyRechargeViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;}
        case 4:{
            SQPointsViewController* vc = [[SQPointsViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;}
        case 5:{
            SQAddressListViewController* vc = [[SQAddressListViewController alloc]init];
            vc.type = @"edit";
            [self.navigationController pushViewController:vc animated:YES];
            break;}
        case 6:{
            SQAboutViewController* vc = [[SQAboutViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;}
        default:
            break;
    }
}

-(void)showbadge
{
    SQMySQL* noticeDB = [[SQMySQL alloc]init];
    sqlTestList* list = [[sqlTestList alloc] init];
    list.username = [Global instance].userinfo.username;
    NSMutableArray* dataMArr = [NSMutableArray array];
    dataMArr = [noticeDB getTestList:list];
    
    if (dataMArr.count > 0) {
        [self.badgeOne setValue:dataMArr.count];
    }else{
        [self.badgeOne setHidden:YES];
    }
    
    [self.badgeTwo setHidden:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)checkLogin
{
    UserInfo* userinfo = [[UserInfo alloc] init];
    userinfo = [userinfo doLoad];
    NSLog(@"%@",userinfo);
    if (userinfo) {
        [Global instance].userinfo = userinfo;
        [self setTitle:@"自动登录"];
    }
    else{
        NSLog(@"lvc");
        SQLoginViewController* lvc = [[SQLoginViewController alloc]init];
        [self.navigationController pushViewController:lvc animated:NO];
    }
}

- (void)doActionQuit:(UIButton*)btn {
    
    user_logout_t logout;
    logout.base.msgtype = 1015;
    logout.base.len = sizeof(logout);
    logout.base.error = 0;
    
    strcpy(logout.userid, [[Global instance].userinfo.username UTF8String]);
    NSLog(@"userid:%s",logout.userid);
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&logout length:sizeof(logout)];
    [super sendData:msgData tag:1 withHUD:NO];
    
    UserInfo* userinfo = [[UserInfo alloc]init];
    [userinfo doDelete];
    [Global instance].logined = false;
    SQLoginViewController* lvc = [[SQLoginViewController alloc]init];
    [self.navigationController pushViewController:lvc animated:NO];
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    return;
    acs_base_t base;
    [data getBytes:&base length:sizeof(base)];
    NSLog(@"out:%d",base.error);
    if(base.error!=ACS_SUCCESS)
    {
        //出错
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            self.HUD.labelText = @"退出失败，请重试";
            sleep(2);
            [self.HUD show:NO];
        } completionBlock:^{
        }];
        return;
    }
    else{
        [Global instance].logined = false;
        SQLoginViewController* lvc = [[SQLoginViewController alloc]init];
        [self.navigationController pushViewController:lvc animated:NO];
    }
}
#pragma mark UIScrollViewDelegate
//只要滚动了就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    //    NSLog(@" scrollViewDidScroll");
    NSLog(@"ContentOffset  x is  %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}

@end
